package com.shen.my_blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shen.my_blog.dao.pojo.SysUser;
import com.shen.my_blog.service.LoginService;
import com.shen.my_blog.service.SysUserService;
import com.shen.my_blog.dao.mapper.SysUserMapper;
import com.shen.my_blog.vo.ErrorCode;
import com.shen.my_blog.vo.LoginUserVo;
import com.shen.my_blog.vo.Result;
import com.shen.my_blog.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private LoginService loginService;

    @Override
    public UserVo findUserVoById(Long id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        if (sysUser == null){
            SysUser sysuser = new SysUser();
            sysUser.setId(1L);
            sysUser.setAvatar("/static/img/logo.b4a48c0.png");
            sysuser.setNickname("火火火木");
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(sysUser,userVo);
        return userVo;
    }


    @Override
    public SysUser selectById(Long id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        if (sysUser == null){
            SysUser sysuser = new SysUser();
            sysuser.setNickname("火火火木");
        }
        return sysUserMapper.selectById(id);
    }


    @Override
    public SysUser findUser(String account, String password) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getAccount,account);
        wrapper.eq(SysUser::getPassword,password);
        wrapper.select(SysUser::getAccount,SysUser::getId,SysUser::getAvatar,SysUser::getNickname);
        wrapper.last("limit 1");

        return sysUserMapper.selectOne(wrapper);
    }

    @Override
    public Result getUserInfoByToken(String token) {
        /**
         * 1、token是否合法
         *      是否为空   解析是否成功  redis是否存在
         * 2、如果校验失败  返回错误
         * 3、如果成功，返回对应的结果 loginVo
         */

        SysUser sysUser = loginService.checkToken(token);
        if (sysUser == null){
            return Result.fail(ErrorCode.TOKEN_ERROR.getCode(),ErrorCode.TOKEN_ERROR.getMsg());
        }
        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setId(sysUser.getId());
        loginUserVo.setNickname(sysUser.getNickname());
        loginUserVo.setAvatar(sysUser.getAvatar());
        loginUserVo.setAccount(sysUser.getAccount());
        return Result.success(loginUserVo);
    }

    @Override
    public SysUser findUserByAccount(String account) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getAccount,account);
        wrapper.last("limit 1");
        return this.sysUserMapper.selectOne(wrapper);
    }


}




